home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Shift Layout ƒ / Shift Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  12.9 KB  |  416 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Shift Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "color library.c", "font library.c", "graphics debug library.c",
  11. |**|    "layout library.c", "shape library.c", and "transform library.c".
  12. |**|
  13. |**|    ©1992-1994  Apple Computer, Inc.
  14. |**|    All rights reserved.
  15. |**|
  16. |**| =====================================================================
  17. \**/
  18.  
  19.  
  20. #include "QDGX shell.h"
  21. #include "layout routines.h"
  22. #include "layout library.h"
  23.  
  24.  
  25. /**\
  26. |**| ---------------------------------------------------------------------
  27. |**| PROTOTYPES
  28. |**| ---------------------------------------------------------------------
  29. \**/
  30.  
  31. // funtions required by shell
  32.  
  33. void    DoSetup            (void);
  34. void    DoDraw            (WindowPtr wind, Boolean updating);
  35. OSErr    DoCreateNew        (void);
  36. void    DoDispose        (WindowPtr wind);
  37. void    DoIdle            (WindowPtr wind);
  38. void    DoTeardown        (void);
  39. void    DoClick            (WindowPtr wind, Point p);
  40.  
  41. // private functions
  42.  
  43. OSErr    DoWindowInit        (WindowPtr wind);
  44. void    CreateSampleImage    (WindowPtr wind);
  45.  
  46.  
  47. /**\
  48. |**| ---------------------------------------------------------------------
  49. |**| ENUMS
  50. |**| ---------------------------------------------------------------------
  51. \**/
  52. enum { rWindResource = 128 };
  53.  
  54.  
  55. /**\
  56. |**| ---------------------------------------------------------------------
  57. |**| GLOBALS
  58. |**| ---------------------------------------------------------------------
  59. \**/
  60. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  61. // functionality will only work with the "debugging" version of QuickDraw GX.
  62. // If the debugging version is not installed, nothing bad will happen, but these
  63. // functions will not work. 
  64.  
  65. Boolean        gDebugging = true;
  66.  
  67. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  68.  
  69. Boolean        gGiveMeValidation = true;
  70.  
  71.  
  72. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  73. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  74. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  75. // since we need abunch if we have several windows open.
  76.  
  77. long        gGraphicsHeapSize = 600;
  78.  
  79. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  80. // override.  This is so that our override can be native PowerPC code if necessary.
  81.  
  82. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  83.  
  84.  
  85.  
  86. /**\
  87. |**| ---------------------------------------------------------------------
  88. |**| DoSetup()
  89. |**| Here's where we initialize any global variables our application needs.
  90. |**| We have only one at this time -- the universal proc pointer for
  91. |**| our printing override.
  92. |**| ---------------------------------------------------------------------
  93. \**/
  94. void DoSetup (void)
  95. {    // Initialize our printing event override UPP
  96.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  97. }
  98.  
  99.  
  100. /**\
  101. |**| ---------------------------------------------------------------------
  102. |**| DoDraw()
  103. |**| Draw the contents of the window.  The first parameter is the window
  104. |**| to draw, and the second parameter is true if we're updating an existing
  105. |**| image.  If that's the case, we don't want to change anything, but
  106. |**| just draw what's already there.
  107. |**| ---------------------------------------------------------------------
  108. \**/
  109. void DoDraw (WindowPtr wind, Boolean updating)
  110. {
  111.      #pragma unused (updating)
  112.      GXDrawShape (GetDocShape(wind));
  113. }
  114.  
  115.  
  116. /**\
  117. |**| ---------------------------------------------------------------------
  118. |**| DoCreateNew()
  119. |**| This routine is called when a window needs to be created.
  120. |**| ---------------------------------------------------------------------
  121. \**/
  122. OSErr DoCreateNew (void)
  123. {
  124.     OSErr        err = noErr;
  125.     WindowPtr    wind;
  126.     
  127. // Get and create our window from the resource fork
  128.  
  129.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  130.  
  131. // Attach a default gxViewPort to it, create and iInitialize our
  132. // private data for it, and add a sample image to its page shape.
  133.  
  134.     if ( wind == NULL )
  135.         return (MemError());
  136.  
  137.     GXIgnoreGraphicsNotice(transform_already_set);
  138.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  139.     GXPopGraphicsNotice();
  140.     
  141.     err = DoWindowInit(wind);
  142.     if ( err != noErr )
  143.         return err;
  144.     
  145.     CreateSampleImage(wind);
  146.     return err;
  147. }
  148.  
  149.  
  150. /**\
  151. |**| ---------------------------------------------------------------------
  152. |**| DoDispose()
  153. |**| This routine is called when a window needs to be disposed of.
  154. |**| ---------------------------------------------------------------------
  155. \**/
  156. void DoDispose (WindowPtr wind)
  157. {
  158.     TH_Doc    doc;
  159.     
  160. // You should always dispose of your GX graphics objects before tossing your window.
  161. // Why?  It's generally good form and this approach guarantees that everything is
  162. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  163. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  164. // with notices set, you will receive a notice that you had not disposed of everything.
  165. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  166.     
  167.     if ( wind != NULL )
  168.     {
  169.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  170.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  171.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  172.         DisposHandle((Handle) doc);            // Dispose of our private data.
  173.         DisposeWindow(wind);                // Dispose of the window.
  174.     }
  175. }
  176.  
  177.  
  178. /**\
  179. |**| ---------------------------------------------------------------------
  180. |**| DoIdle()
  181. |**| This routine is called to do things while idling through the event loop.
  182. |**| ---------------------------------------------------------------------
  183. \**/
  184. void DoIdle (WindowPtr wind)
  185. {
  186. }
  187.  
  188.  
  189. /**\
  190. |**| ---------------------------------------------------------------------
  191. |**| DoTeardown()
  192. |**| This routine is called just before we quit to remove anything 
  193. |**| persistent that might have been setup by DoSetup().
  194. |**| ---------------------------------------------------------------------
  195. \**/
  196. void DoTeardown (void)
  197. {
  198.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  199. }
  200.  
  201.  
  202. /**\
  203. |**| ---------------------------------------------------------------------
  204. |**| DoClick()
  205. |**| ---------------------------------------------------------------------
  206. \**/
  207. void DoClick(WindowPtr window, Point p)
  208. {
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. /**\
  216. |**| ---------------------------------------------------------------------
  217. |**| DoWindowInit()
  218. |**| In this function we create and initialize the the private document
  219. |**| structure for a new window.  This structure contains the print job and
  220. |**| the shape which is drawn in the window.  We store this data in a handle
  221. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  222. |**| this, rather than using globals, we can create many windows containing
  223. |**| unique print jobs and shapes.
  224. |**| ---------------------------------------------------------------------
  225. \**/
  226. OSErr DoWindowInit (WindowPtr wind)
  227. {
  228.     OSErr    err = noErr;
  229.     gxJob    docJob;
  230.     gxShape    docPage;
  231.     TH_Doc    windDoc;
  232.  
  233.  
  234. // Create the page shape. We set the unique items attribute to make sure that each item
  235. // added to the picture has a unique reference. If this attribute was not set, we would
  236. // not see all copies of anything we add to the shape multiple times -- we'd just see
  237. // the last version added.        
  238.  
  239.     docPage = GXNewShape(gxPictureType);
  240.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  241.     
  242.     
  243. // Create a print job for this document.  This will be the same as the system default until
  244. // the user goes through the dialogs for Page Setup or Print…
  245.  
  246.     err = GXNewJob(&docJob);
  247.     
  248.     
  249. // If there are no errors, create a handle the size of our document structure and store
  250. // the print job and page shape in it.  Store the handle in the window's refCon field so
  251. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  252. // can be used to do this easily.
  253.  
  254.     if ( err == noErr )
  255.     {
  256.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  257.  
  258.         if ( windDoc == NULL )
  259.             err = MemError();
  260.         else
  261.         {
  262.             (*windDoc)->docJob = docJob;
  263.             (*windDoc)->docPage = docPage;
  264.             SetWRefCon(wind, (long) windDoc);
  265.         }
  266.  
  267. // Now install our application override for PrintingEvent so that we can
  268. // support the new movable-modal printing dialog boxes.
  269.  
  270.         GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
  271.  
  272.     }
  273.  
  274.     return err;
  275. }
  276.  
  277.  
  278. /**\
  279. |**| ---------------------------------------------------------------------
  280. |**| CreateSampleImage()
  281. |**| This function creates primitive shapes and adds them to the window's page shape.
  282. |**| ---------------------------------------------------------------------
  283. \**/
  284. void CreateSampleImage (WindowPtr wind)
  285. {
  286.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  287.                                             // in QuickDraw coordinates
  288.     gxShape layout;                        // the layout shape we build
  289.     gxShape thePage;                    // this window's document shape
  290.     gxRunControls runControls;            // run controls for the layout shape
  291.     gxLayoutOptions layoutOptions;        // options for the layout shape
  292.     gxStyle textStyles[5];                // an array of text style for our text runs
  293.     
  294. // These are the five text runs for this layout shape, in pieces because they're
  295. // in different languages
  296.     
  297.     char *text1 = "Office";
  298.     
  299. // The following is "Macintosh" in Arabic: 
  300. // meem, alif, kaf,  noon, tah,  wau,  shin
  301. // (This assumes the standard Arabic character set for Macintosh, by the way)
  302.     
  303.     char *text2 = "AWAY";
  304.     
  305.     char *textRuns[2];                    // an array of pointers to text runs
  306.  
  307.     short textLengths[2];                // an array of the lengths of each run
  308.     short totalLength;                    // the total length of the layout's text
  309.     short level0 = 0;                    // variable to take address of later
  310.     
  311.     gxPoint posn;                        // the position of the layout shape
  312.  
  313.  
  314. // First, initialize the textRuns array to point to the two text runs
  315.     
  316.     textRuns[0] = text1;
  317.     textRuns[1] = text2;
  318.     
  319. // Next, initialize the textLengths and levelRunLengths arrays.  MyStrLength() is
  320. // in this file.
  321.  
  322.     textLengths[0] = MyStrLength (text1);
  323.     textLengths[1] = MyStrLength (text2);
  324.     
  325. // totalLength is the length of all the text.
  326.  
  327.     totalLength = textLengths[0] + textLengths[1];
  328.     
  329.  
  330. // make default gxLayoutOptions and gxRunControls structures
  331.  
  332.     InitializeLayoutOptions (&layoutOptions);
  333.     InitializeRunControls (&runControls);
  334.     
  335.     
  336. // Position the layout half way down the left edge of the window. Set 
  337. // the layout's width to the window's width and set the flushness to 1/2, this
  338. // will cause the layout to center in the window.  Note that ourWindowRect is
  339. // not in fixed coordinates, so we have to make it fixed to use it.
  340.  
  341.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  342.     layoutOptions.flush = fract1/2;
  343.  
  344.     posn.x = 0;
  345.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  346.     
  347.  
  348. // Create the styles we'll use. 
  349. // NewLayoutStyle is a library routine found in layout library.c.  
  350.     
  351.     textStyles[0] = NewLayoutStyle(
  352.         (char *) "\pHoefler Text",            // the gxFontName for the style
  353.         ff(60),                                // the text size in fixed point
  354.         0,                                    // gxTextAttributes
  355.         &runControls,                        // run controls (our default ones)
  356.         nil,                                // run features (none for this style)
  357.         0,                                    // count of run features
  358.         nil);                                // style run overrides (none right now)
  359.  
  360. // Now set the run controls to add 30 points cross-stream kerning and -4 points
  361. // with-stream kerning, which moves the text four points closer together than
  362. // normal kerning would.
  363.  
  364.     runControls.crossStreamShift = ff(30);
  365.     runControls.beforeWithStreamShift = ff(-4);
  366.  
  367. // Now make another style with our new shifted kerning
  368.  
  369.     textStyles[1] = NewLayoutStyle(
  370.         (char *) "\pHoefler Text",            // the gxFontName for the style
  371.         ff(30),                                // the text size in fixed point
  372.         0,                                    // gxTextAttributes
  373.         &runControls,                        // run controls (our default ones)
  374.         nil,                                // run features (none for this style)
  375.         0,                                    // count of run features
  376.         nil);                                // style run overrides (none right now)
  377.     
  378.  
  379. // Setup complete!  Build the layout.
  380.  
  381.     layout = GXNewLayout(
  382.         2,                                // count of text runs
  383.         textLengths,                    // array of lengths of each run
  384.         (void *) textRuns,                // array of pointers to the text
  385.         2,                                // count of style runs
  386.         textLengths,                    // array of the byte lengths of each run
  387.         textStyles,                        // array of the styles
  388.         1,                                // number of levels in the layout
  389.         &totalLength,                    // array of the lengths of each level
  390.         &level0,                        // array of the levels
  391.         &layoutOptions,                    // options (default)
  392.         &posn);                            // position of the layout shape
  393.  
  394.     
  395. // Retrieve the page shape so we can add to it.
  396.  
  397.     thePage = GetDocShape(wind);
  398.     
  399. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  400.  
  401.     AddToShape(thePage, layout);
  402.  
  403. // Dispose of what we created
  404.  
  405.     GXDisposeShape(layout);
  406.     GXDisposeStyle(textStyles[0]);
  407.     GXDisposeStyle(textStyles[1]);
  408.     
  409.  
  410. // Invalidate the window's portRect so that everything gets updated.
  411.     
  412.     SetPort(wind);
  413.     InvalRect(&ourWindowRect);
  414. }
  415.  
  416.